#!/usr/bin/python
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
#cron: 12 6,20 * * *
import datetime
import time
import requests
import re
import json
import random

time2 = time.strftime('%Y-%m-%d ', time.localtime(time.time()))
today = datetime.date.today()  # 获取当前日期, 因为要求时分秒为0, 所以不要求时间
weekday = today.weekday()  # 获取当前周的排序, 周一为0, 周日为6
monday_delta = datetime.timedelta(weekday)  # 当前日期距离周一的时间差
Tuesday_delta = datetime.timedelta(weekday - 1)
Wednesday_delta = datetime.timedelta(weekday - 2)
Thursday_delta = datetime.timedelta(weekday - 3)
Friday_delta = datetime.timedelta(weekday - 4)
Saturday_delta = datetime.timedelta(weekday - 5)
Sunday_delta = datetime.timedelta(weekday - 6)
# sunday_delta = datetime.timedelta(7 - weekday)    # 当前日期距离下周一的时间差
Monday = today - monday_delta  # 获取这周一日期
Tuesday = today - Tuesday_delta
Wednesday = today - Wednesday_delta
Thursday = today - Thursday_delta
Friday = today - Friday_delta
Saturday = today - Saturday_delta
Sunday = today - Sunday_delta
xq1 = f'{Monday}'
xq2 = f'{Tuesday}'
xq3 = f'{Wednesday}'
xq4 = f'{Thursday}'
xq5 = f'{Friday}'
xq6 = f'{Saturday}'
xq7 = f'{Sunday}'
# next_monday = today + sunday_delta    # 获取下周一日期
print(Monday)
print(Tuesday)
print(Wednesday)
print(Thursday)
print(Friday)
print(Saturday)
print(Sunday)
print(weekday)
print(today)

headers = {
    'Host': 'safe.jx.edu.cn',
    'Connection': 'keep-alive',
    'Accept': 'application/json, text/javascript, */*; q=0.01',
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36 NetType/WIFI MicroMessenger/7.0.20.1781(0x6700143B) WindowsWechat(0x6307001a)',
    'X-Requested-With': 'XMLHttpRequest',
    'Cookie': 'openId=o6uEIwSv4g3lknsDa1UyH5xwMXDo; sid=0890c987-615c-4382-900d-e1148de214ea',
    'Referer': 'http://safe.jx.edu.cn/wechat.html?openId=o6uEIwSv4g3lknsDa1UyH5xwMXDo',
    'Accept-Encoding': 'gzip, deflate',
    'Accept-Language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',


}
headers1 = {
    'Host': 'safe.jx.edu.cn',
    'Connection': 'keep-alive',
    'Content-Length': '67',
    'Accept': '*/*',
    'Origin': 'http://safe.jx.edu.cn',
    'X-Requested-With': 'XMLHttpRequest',
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36 NetType/WIFI MicroMessenger/7.0.20.1781(0x6700143B) WindowsWechat(0x6307001a)',
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    'Cookie': 'openId=o6uEIwSv4g3lknsDa1UyH5xwMXDo; sid=0890c987-615c-4382-900d-e1148de214ea',
    'Referer': 'http://safe.jx.edu.cn/wechat.html?openId=o6uEIwSv4g3lknsDa1UyH5xwMXDo',
    'Accept-Encoding': 'gzip, deflate',
    'Accept-Language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',


}

global roles
global datas
global checkstate
global token


def lin():
    # xianzai6 = time.strftime('%Y-%m-%d %H:%M:%S ',time.localtime(time.time()))
    # xianzai8 = f'logintime:{xianzai6}'
    # number = random.randint(1,3)
    # print(f'随机延迟:{number}秒')
    # yanchi = f'检测到随机延迟,将在延迟{number}秒后签到,'
    # print(xianzai8)
    # time.sleep(number)
    url = f'http://safe.jx.edu.cn/api/loginByOpenId?openId=o6uEIwSv4g3lknsDa1UyH5xwMXDo'
    response = requests.get(url=url, headers=headers)
    msg = response.json()
    schoolName = msg['data']['school']['schoolName']
    token = msg['data']['token']
    datas = {"state": "0", "content": " ", "userId": "", "token": {token}}
    roles = msg['data']['roles']
    ifsign = msg['message']
    print(f'signStatus:{ifsign}')
    denglu = f'signstatus:{ifsign}'
    print(f'schoolName:{schoolName}')
    xuexiao = f'schoolName:{schoolName}'
    print(f'roles:{roles}')
    juese = f'roles:{roles}'
    print(f'token:{token}')
    tokens = f'token:{token}'
    realname = f'realname:林祥'
    print(realname)
    urlcheck = f'http://safe.jx.edu.cn/api/signCheck?token={token}&id='
    check = requests.get(url=urlcheck, headers=headers)
    checkinfomation = check.json()
    checkstatus = (checkinfomation['message'])
    print(f'checkinstatus:{checkstatus}')
    qiandao1 = f'checkinstatus:{checkstatus}'
    now_time = time.strftime('%Y-%m-%d %H:%M:%S ', time.localtime(time.time()))
    print(f'logintime:{now_time}')
    xianzai = f'logintime:{now_time}'
    week2 = f'正在查询本周签到情况...'
    week3 = f'查询成功！(0:已签到,None:未到时间,1:未签到)'
    startdate = '2022-09-01'
    enddate = time.strftime('%Y-%m-%d', time.localtime(time.time()))
    print(startdate, enddate)
    if checkinfomation['message'] == "已签到":
        # signtime = checkinfomation['data']['signTime']
        print(f'signtime:{now_time}')
        shijian = f'signtime:{now_time}'
        url3 = f'http://safe.jx.edu.cn/api/signCheckWeek?id=&date={time2}&token={token}'
        weeks = requests.get(url=url3)
        week = weeks.text
        print(week)
        yi = weeks.json()['data'][f'{Monday}']
        er = weeks.json()['data'][f'{Tuesday}']
        san = weeks.json()['data'][f'{Wednesday}']
        si = weeks.json()['data'][f'{Thursday}']
        wu = weeks.json()['data'][f'{Friday}']
        liu = weeks.json()['data'][f'{Saturday}']
        qi = weeks.json()['data'][f'{Sunday}']
        yi1 = f'{yi}'
        er2 = f'{er}'
        san3 = f'{san}'
        si4 = f'{si}'
        wu5 = f'{wu}'
        liu6 = f'{liu}'
        qi7 = f'{qi}'
        url6 = f'http://safe.jx.edu.cn/api/syntstatistics/area/danger/count?queryFlag=1&startDate={startdate}&endDate={enddate}&areaId=&provinceFlag=0&token={token}'
        tongji = requests.get(url6)
        tongji1 = tongji.text
        tongji2 = tongji.json()['message']
        tongji3 = f'querysign:{tongji2}'
        tongji6 = tongji.json()['data'][0]['signSum']
        tongji8 = f'尝试查询本学期签到天数……'
        kaishichaxun = f'startDate:{startdate}'
        jiesuchaxun = f'endDate:{enddate}'
        signSum = f'signSum:{tongji6}天'
        print(tongji3, kaishichaxun, jiesuchaxun, tongji6)
        print(tongji1)
        tongji10 = '<br/>' + '<hr/>' + tongji8 + '<br/>' + tongji3 + '<br/>' + kaishichaxun + '<br/>' + jiesuchaxun + '<br/>' + signSum
        # print(Monday)
        print(f'\n{Monday}\n{yi} \n{Tuesday}\n {er} \n{Wednesday}\n{san}')
        aa = xq1 + '<br/>' + yi1 + '<br/>' + xq2 + '<br/>' + er2 + '<br/>' + xq3 + '<br/>' + san3 + '<br/>' + xq4 + '<br/>' + si4 + '<br/>' + xq5 + '<br/>' + wu5 + '<br/>' + xq6 + '<br/>' + liu6 + '<br/>' + xq7 + '<br/>' + qi7
        print(aa)
        tui = denglu + '<br/>' + xuexiao + '<br/>' + juese + '<br/>' + realname + '<br/>' + tokens + '<br/>' + qiandao1 + '<br/>' + shijian + '<br/>' + '<hr/>' + 'Congratulations!Today has completed the checkin!' + tongji10 + '<br/>' + '<hr/>' + week2 + '<br/>' + week3 + '<br/>' + '<hr/>' + aa
    else:
        number = random.randint(600, 1000)
        print(f'随机延迟:{number}秒')
        yanchi = f'检测到随机延迟,将在延迟{number}秒后签到,'
        time.sleep(number)
        xianzai1 = time.strftime('%Y-%m-%d %H:%M:%S ', time.localtime(time.time()))
        xianzai2 = f'signtime:{xianzai1}'
        print(xianzai1)
        urlsign = f'http://safe.jx.edu.cn/api/sign/'
        trysign = requests.post(url=urlsign, data=datas, headers=headers1)
        tuisong = trysign.text
        print(tuisong)
        qiandao2 = trysign.json()['message']
        print(f'checkinresult:{qiandao2}')
        qiandao3 = f'checkinresult:{qiandao2}'
        # signtime = tuisong['data']['signTime']
        print(f'signtime:', time.strftime('%Y.%m.%d %H:%M:%S ', time.localtime(time.time())))
        shijian = f'nowtime:{now_time}'
        try2 = f'未签到，尝试签到中，请稍等……'
        url3 = f'http://safe.jx.edu.cn/api/signCheckWeek?id=&date={time2}&token={token}'
        weeks = requests.get(url=url3)
        week = weeks.text
        print(week)
        yi = weeks.json()['data'][f'{Monday}']
        er = weeks.json()['data'][f'{Tuesday}']
        san = weeks.json()['data'][f'{Wednesday}']
        si = weeks.json()['data'][f'{Thursday}']
        wu = weeks.json()['data'][f'{Friday}']
        liu = weeks.json()['data'][f'{Saturday}']
        qi = weeks.json()['data'][f'{Sunday}']
        yi1 = f'{yi}'
        er2 = f'{er}'
        san3 = f'{san}'
        si4 = f'{si}'
        wu5 = f'{wu}'
        liu6 = f'{liu}'
        qi7 = f'{qi}'
        bb = xq1 + '<br/>' + yi1 + '<br/>' + xq2 + '<br/>' + er2 + '<br/>' + xq3 + '<br/>' + san3 + '<br/>' + xq4 + '<br/>' + si4 + '<br/>' + xq5 + '<br/>' + wu5 + '<br/>' + xq6 + '<br/>' + liu6 + '<br/>' + xq7 + '<br/>' + qi7
        url6 = f'http://safe.jx.edu.cn/api/syntstatistics/area/danger/count?queryFlag=1&startDate={startdate}&endDate={enddate}&areaId=&provinceFlag=0&token={token}'
        tongji = requests.get(url6)
        tongji1 = tongji.text
        tongji2 = tongji.json()['message']
        tongji3 = f'querysign:{tongji2}'
        tongji6 = tongji.json()['data'][0]['signSum']
        tongji8 = f'尝试查询本学期签到天数……'
        kaishichaxun = f'startDate:{startdate}'
        jiesuchaxun = f'endDate:{enddate}'
        signSum = f'signSum:{tongji6}天'
        print(tongji3, kaishichaxun, jiesuchaxun, tongji6)
        print(tongji1)
        tongji10 = '<br/>' + '<hr/>' + tongji8 + '<br/>' + tongji3 + '<br/>' + kaishichaxun + '<br/>' + jiesuchaxun + '<br/>' + signSum
        tui = denglu + '<br/>' + xianzai + '<br/>' + xuexiao + '<br/>' + juese + '<br/>' + realname + '<br/>' + tokens + '<br/>' + qiandao1 + '<br/>' + '<hr/>' + try2 + '<br/>' + yanchi + '<br/>' + shijian + '<br/>' + '<hr/>' + qiandao3 + '<br/>' + xianzai2 + '<br/>' + '<hr/>' + 'Congratulations!Today has completed the checkin!' + tongji10 + '<br/>' + '<hr/>' + week2 + '<br/>' + week3 + '<br/>' + '<hr/>' + bb
        print(tui)
    # print(checkinfomation)

    # now_time = datetime.datetime.now()
    # print(now_time)
    # urlsign = f'http://safe.jx.edu.cn/api/sign/'
    # trysign = requests.post(url=urlsign, data=datas, headers=headers)
    # tuisong = trysign.text
    # print(tuisong)
    # tui = denglu + '<br/>' +xuexiao + '<br/>' +juese + '<br/>' +realname
    #  pushdata = { 'token': 'c965d00470284ef785662292022b8455', 'title':'安全平台每日签到通知', 'content':lin, 'template':'text'}
    # pushplshurl = f'http://pushplus.hxtrip.com/send'
    # pushsign = requests.post(url = pushplshurl, data = pushdata)
    # print(pushsign.text)
    requests.get(
        f"http://pushplus.hxtrip.com/send?token=c965d00470284ef785662292022b8455&title=校园安全每日签到&content={tui}&template=html")
    requests.get(
        f"http://www.pushplus.plus/send?token=ca430ae251004ffabc58e6467c5770f4&title=校园安全每日签到&content={tui}&template=html")


lin()